package com.sinodata.bsm.cicp.collector.pos;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.CollectorUtil;
import com.sinodata.bsm.common.vo.ResPropertyValue;

public class POSCollector extends Collector {

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();

        BufferedReader reader = null;
        String cmd = getParamMap().get("commond");
        String ptnStr = getParamMap().get("Regex");
        long value1 = Long.parseLong(getParamMap().get("value1"));
        long value2 = Long.parseLong(getParamMap().get("value2"));
        try {
            reader = CollectorUtil.getReader(cmd, ip);
            String line = null;
            Pattern p = Pattern.compile(ptnStr);
            Matcher match = null;
            while ((line = reader.readLine()) != null) {
                match = p.matcher(line);
                if (match.matches()) {
                    if (Long.parseLong(match.group(2)) > value1 || Long.parseLong(match.group(3)) > value2) {
                        ResPropertyValue propertyValueX = new ResPropertyValue();
                        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
                        propValues[0] = ResPropertyValue.createPropValue(match.group(1), propMetas[0]);
                        propValues[1] = ResPropertyValue.createPropValue(match.group(2), propMetas[1]);
                        propValues[2] = ResPropertyValue.createPropValue(match.group(3), propMetas[2]);
                        propertyValueX.setValues(propValues);
                        values.add(propertyValueX);
                    }
                }
            }
            if (values.size() == 0) {
                ResPropertyValue propertyValueX = new ResPropertyValue();
                ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
                propValues[0] = ResPropertyValue.createPropValue("N/A", propMetas[0]);
                propValues[1] = ResPropertyValue.createPropValue("N/A", propMetas[1]);
                propValues[2] = ResPropertyValue.createPropValue("N/A", propMetas[2]);
                propertyValueX.setValues(propValues);
                values.add(propertyValueX);
            }
        } catch (IOException e) {
            logger.error("collect POS disconnect:" + e);
            throw new CollectorException(e);
        }
        return values;
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "value1", "value2", "commond", "Regex" };
    }

    @Override
    public String[] getPropMeta() {
        return new String[] { "Pmunber", "value1", "value2" };
    }

}
